#ifndef TRAFFICCONTROL_H
#define TRAFFICCONTROL_H

#include <QList>
#include "streetmap.h"

class TrafficControl
{
  public:

    // TrafficControl PUBLIC TYPES
    enum CONTROL_TYPE {
      TYPE_NONE,
      TYPE_STOPSIGN,
      TYPE_LIGHT
    };

    enum LIGHT_COLOR {
      LIGHT_RED,
      LIGHT_YELLOW,
      LIGHT_GREEN
    };

    class Connection
    {
    public:
      Connection(const StreetMap::Road *from, const unsigned int fromLane,
                 const StreetMap::Road *to, const unsigned int toLane);
      const StreetMap::Road *from;
      unsigned int fromLane;
      const StreetMap::Road *to;
      unsigned int toLane;
    };

    typedef QList<Connection*> ConnectionList;

    class State
    {
    public:
      State(const QString &name);
      ~State();
      void setName(const QString &name);
      QString name() const;
      ConnectionList getConnections();
      void addConnection(Connection *connection);
      void setGreenTime(const qreal timeInSeconds);
      qreal greenTime() const;
      void setYellowTime(const qreal timeInSeconds);
      qreal yellowTime() const;
      void setPostRedTime(const qreal timeInSeconds);
      qreal postRedTime() const;
      LIGHT_COLOR lightColor(const StreetMap::Road *from, const unsigned int fromLane,
                             const StreetMap::Road *to, const unsigned int toLane) const;
      LIGHT_COLOR lightColor() const; // this isn't really useful
      void setLightColor(const LIGHT_COLOR color);
      bool operator==(const State &other) const;

    protected:
      QString mName;
      ConnectionList mConnections;
      qreal mGreenTime;
      qreal mYellowTime;
      qreal mPostRedTime;
      LIGHT_COLOR mCurrentColor;
    };

    // TrafficControl CONSTRUCTOR/DESTRUCTOR
    TrafficControl();
    ~TrafficControl();

    // TrafficControl PUBLIC METHODS
    void setIntersection(const StreetMap::Intersection *intersection);
    const StreetMap::Intersection* intersection() const;
    void addState(State *state);
    //State getState(const QString &name) const;
    State* currentState() const;
    void setType(const CONTROL_TYPE type);
    CONTROL_TYPE type() const;
    void tick();

  protected:
    // TrafficControl PROTECTED MEMBERS
    QList<State*> mStates;
    const StreetMap::Intersection *mIntersection;
    CONTROL_TYPE mType;
    int mCurrentStateIndex;
    qreal mTimeSinceStateChange;
};

#endif // TRAFFICCONTROL_H
